home *** CD-ROM | disk | FTP | other *** search
/ Aminet 23 / Aminet 23 (1998)(GTI - Schatztruhe)[!][Feb 1998].iso / Aminet / disk / misc / TransADF.lha / Source / util.c < prev    next >
C/C++ Source or Header  |  1997-12-06  |  6KB  |  265 lines

  1. /*------------------------------------*/
  2. /* Miscellaneous functions and macros */
  3. /*------------------------------------*/
  4.  
  5. #include <exec/types.h>
  6. #ifndef COMPILE_LITE
  7. #  include <dos/dos.h>
  8. #  include <clib/dos_protos.h>
  9.  
  10. #  include <time.h>
  11. #endif /* COMPILE_LITE */
  12.  
  13. #include <string.h>
  14. #include <ctype.h>
  15.  
  16. #include "util.h"
  17. #ifndef COMPILE_LITE
  18. #  include "gzip.h"
  19. #  include "pkzip.h"
  20. #endif /* COMPILE_LITE */
  21.  
  22.  
  23. /*
  24. ** Determine the unit number of a trackdisk device (DFx).
  25. ** Returns -1L if device name is not valid.
  26. ** devName should be of the format "DFx:\0", where x is a digit.
  27. */
  28. LONG Name2Unit (STRPTR devName)
  29. {
  30.   if ( (tolower(devName[0]) == 'd') && (tolower(devName[1]) == 'f') &&
  31.        (devName[2] >= '0') && (devName[2] <= '3') &&
  32.        (devName[3] == ':') && (devName[4] == '\0')
  33.      )
  34.     return devName[2] - '0';
  35.   
  36.   return (-1);
  37. }
  38.  
  39.  
  40. /* The following routines are only used in conjunction with de/compression */
  41. #ifndef COMPILE_LITE
  42.  
  43. /* 
  44. ** Return the file type, based on the 'magic number'
  45. ** ie the first few charcters.
  46. ** This function preserves current file position.
  47. */
  48. ULONG getFileType (BPTR file)
  49. {
  50.   UBYTE mag_num[4];
  51.   static UBYTE pkzip_mag[4] = {'P', 'K', 0x03, 0x04};
  52.   static UBYTE gzip_mag[2]  = {0x1F, 0x8B};
  53.   static UBYTE dos_mag[3]   = {'D',  'O',  'S'};
  54.   LONG oldp;
  55.   
  56.   oldp = Seek (file, 0, OFFSET_BEGINNING);
  57.   if ( Read (file, mag_num, 4) != 4) return FT_UNKNOWN;
  58.   Seek (file, oldp, OFFSET_BEGINNING);
  59.   
  60.   /* These are straight-forward comparisons */
  61.   if (!memcmp (mag_num, pkzip_mag, 4))
  62.     return FT_PKZIP;
  63.   if (!memcmp (mag_num, gzip_mag, 2))
  64.     return FT_GZIP;
  65.   if (!memcmp (mag_num, dos_mag, 3))
  66.     return FT_DOS;
  67.   
  68.   /* Zlib is a little tricky - The first char is usually 0x78, and the */
  69.   /* first two chars, when viewed together as an unsigned 16-bit int,  */
  70.   /* is a multiple of 31.                                              */
  71.   /* This probably needs a litte more work :)                          */
  72.   if ((mag_num[0] == 0x78) && !(((UWORD *)mag_num)[0] % 31))
  73.     return FT_ZLIB;
  74.   
  75.   /* If we get here, the type is unknown */
  76.   return FT_UNKNOWN;
  77. }
  78.  
  79.  
  80. /*
  81. ** Output a Header to the specified file depending on fileType.
  82. ** Return TRUE if no errors. else FALSE.
  83. */
  84. BOOL writeHead (BPTR outFile, STRPTR origName, ULONG fileType)
  85. {
  86.   /* Assume that the file is at the start */
  87.  
  88.   switch (fileType)
  89.   {
  90.   case FT_ZLIB:
  91.     /* No action necessary */
  92.     return TRUE;
  93.   
  94.   case FT_GZIP:
  95.     return writeGZHead (outFile, origName);
  96.   
  97.   case FT_PKZIP:
  98.     return writePKZHead (outFile, origName);
  99.   
  100.   case FT_PKZIP_ADD:
  101.     return writePKZHeadAdd (outFile, origName);
  102.   }
  103.   
  104.   /* Unknown type, error! */
  105.   return FALSE;
  106. }
  107.  
  108.  
  109. /*
  110. ** Finish writing a file depending on fileType.
  111. ** Return TRUE if no errors. else FALSE.
  112. */
  113. BOOL finishFile (BPTR outFile, ULONG CRC, ULONG CSize, ULONG USize, 
  114.                 ULONG fileType)
  115. {
  116.   switch (fileType)
  117.   {
  118.   case FT_ZLIB:
  119.     /* No action necessary */
  120.     return TRUE;
  121.   
  122.   case FT_GZIP:
  123.     return finishGZFile (outFile, CRC, USize);
  124.   
  125.   case FT_PKZIP:
  126.     return finishPKZFile (outFile, CRC, CSize, USize);
  127.     
  128.   case FT_PKZIP_ADD:
  129.     return finishPKZFileAdd (outFile, CRC, CSize, USize);
  130.   }
  131.   
  132.   /* Unknown type, error! */
  133.   return FALSE;
  134. }
  135.  
  136.  
  137. /*
  138. ** Skip the header of a specified file depending on file type.
  139. ** Return TRUE if no errors. else FALSE.
  140. */
  141. BOOL skipHead (BPTR inFile, STRPTR origName, ULONG fileType)
  142. {
  143.   /* Assume that the file is at the start */
  144.  
  145.   switch (fileType)
  146.   {
  147.   case FT_ZLIB:
  148.     /* No action necessary */
  149.     return TRUE;
  150.   
  151.   case FT_GZIP:
  152.     return skipGZHead (inFile);
  153.   
  154.   case FT_PKZIP:
  155.     return skipPKZHead (inFile, origName);
  156.   }
  157.   
  158.   /* Unknown type, error! */
  159.   return FALSE;
  160. }
  161.  
  162.  
  163. /*
  164. ** Read the a file depending on fileType and return the CRC
  165. ** and USize in supplied arrays.
  166. ** Return TRUE if no errors, else FALSE.
  167. */
  168. BOOL readTail (BPTR inFile, ULONG *CRC, ULONG *USize, ULONG fileType)
  169. {
  170.   switch (fileType)
  171.   {
  172.   case FT_ZLIB:
  173.     /* No action necessary */
  174.     return TRUE;
  175.   
  176.   case FT_GZIP:
  177.     return readGZTail (inFile, CRC, USize);
  178.   
  179.   case FT_PKZIP:
  180.     return readPKZTail (inFile, CRC, USize);
  181.   }
  182.   
  183.   /* Unknown type, error! */
  184.   return FALSE;
  185. }
  186.  
  187.  
  188. /*
  189. ** Return the current date and time in UNIX format.
  190. ** (ie No. seconds after 1-Jan-1970).
  191. */
  192. ULONG unixDate (void)
  193. {
  194.   struct DateStamp ds;
  195.   
  196.   DateStamp (&ds);
  197.   
  198.   /* Need to add 2922 Days (1970->1978) */
  199.   
  200.   return ((ds.ds_Tick / TICKS_PER_SECOND) + 
  201.           (ds.ds_Minute * 60) +
  202.           ((ds.ds_Days + 2922) * 86400));
  203. }
  204.  
  205.  
  206. /*
  207. ** Return the current date and time in DOS format.
  208. */
  209. ULONG  dosDate (void)
  210. {
  211.   /* The DOS date format is as follows:
  212.    * 7 bits: Year after 1980 (0 - 127).
  213.    * 4 bits: Month (1 - 12).
  214.    * 5 bits: Day (1 - 31).
  215.    * (16 bits)
  216.    * 
  217.    * 5 bits: Hour (0 - 23).
  218.    * 6 bits: Minute (0 - 59).
  219.    * 5 bits: Seconds / 2 (0 - 29).
  220.    * (16 bits)
  221.    * 
  222.    * Total = 32 bits = 1 long word.
  223.    */
  224.   
  225.   time_t t;
  226.   struct tm *tp;
  227.   
  228.   /* Get the time */
  229.   t = time (NULL); tp = localtime (&t);
  230.   
  231.   /* Adjust year to within DOS range */
  232.   if (tp->tm_year < 80) return 0x00210000;   /* 1-1-1980, 00:00:00 */
  233.   
  234.   return (((tp->tm_year - 80) << 25) | ((tp->tm_mon+1) << 21) | 
  235.           (tp->tm_mday << 16) | (tp->tm_hour << 11) | (tp->tm_min << 5) |
  236.           (tp->tm_sec >> 1));
  237. }
  238.  
  239.  
  240. /*
  241. ** Return little-endian format of supplied short.
  242. ** Can also be used to convert a short from little-endian.
  243. */
  244. UWORD LES (UWORD num)
  245. {
  246.   return (((num & 0xFF00) >> 8) |
  247.           ((num & 0x00FF) << 8));
  248. }
  249.  
  250.  
  251. /*
  252. ** Return little-endian format of supplied long.
  253. ** Can also be used to convert a long from little-endian.
  254. */
  255. ULONG LEL (ULONG num)
  256. {
  257.   return (((num & 0xFF000000) >> 24) |
  258.           ((num & 0x00FF0000) >> 8)  |
  259.           ((num & 0x0000FF00) << 8)  |
  260.           ((num & 0x000000FF) << 24));
  261. }
  262.  
  263.  
  264. #endif /* COMPILE_LITE */
  265.